#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>


using namespace std;

class Words
{
public:
	typedef pair<const string, int> value;
	map<string,int> *wordMap;
	


	Words(void)
	{
		this->wordMap = new map<string,int>();
	}

	~Words(void)
	{
	}

	void ReadFromFile( string filename )
	{
		ifstream fin;
		string str, strRow;

		try
		{
			fin.open( filename );

			while( getline( fin, strRow ) )
			{
				istringstream iss( strRow );


				while( getline( iss, str, ' ' ) )
				{
					if( str[str.size() -1] == '.' || str[str.size() -1] == ',' )
						str[str.size() -1] = NULL;
					if( wordMap->find(str) == wordMap->end() )
						(*wordMap)[str] = 1;
					else
						(*wordMap)[str] += 1;
				}
			}
		}
		catch( exception e )
		{
			cout << "Exception" << filename << endl;
			cout << e.what() << endl;
		}

		//cout << "Done reading" << endl;

		fin.close();
	}

	void MostFrequentWord()
	{
		int currentMax = 0;
		string maxWord;

		for( auto it = wordMap->cbegin(); it != wordMap->cend(); it++ )
		{
			if( it->second > currentMax )
			{
				currentMax = it->second;
				maxWord = it->first;
			}
		}

		cout << "Most used word is: " << maxWord << endl;
		cout << maxWord << " was used: " << currentMax << " times." << endl;





		//auto max = max_element(wordMap->begin(), wordMap->end(), wordMap->value_comp());
		//cout << max->first << "=>" << max->second << endl;
	}

};

